<?php

/*
 * Copyright 2017 zhc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

if(!defined('MY_ROOT'))
{die();}

/**
 * Editing the set of books
 * @package Library
 * @author Zhu Chen
 */
class Library extends TableEditor{
    public function __construct() {
        parent::__construct('books');
        $this->title = _('Book List');
        $this->qsearch_col = 'name';
        $this->key_col = 'id';
        $this->btnadd_display = _('Add Book');
        $this->btndel_display = _('Delete Book');
        $this->btnmod_display = _('Edit Book');
        $this->btnsearch_display = _('Search Book');
        
        $this->init_columns();
    }
    
    /**
     * Initial columns of the table editor
     */
    private function init_columns()
    {
        $cols =[];$i=0;
        $cols[$i++] = new BookIDCol();
        $cols[$i++] = new BookNameCol();
        $cols[$i++] = new BookAuthorCol();
        $cols[$i++] = new BookPubCol();
        $cols[$i++] = new BookIsbnCol();
        $cols[$i++] = new BookPriceCol();
        $cols[$i++] = new BookRentCol();
        $cols[$i++] = new BookDescriptionCol();
        $this->columns = $cols;
    } 
    
    /**
     * Override the function in table editor to make it more function
     * @param string|int $condition
     * @param string $order DESC or ASC
     * @return array The result
     */
    public function qsearch($condition, string $order = 'DESC') {
        //Check if is isbn
        $rslt = [];
        $o = in_array($order, ['ASC','DESC']) ? $order : 'DESC';
        // First ISBN code
        if((strlen($condition)==13)&&(substr($condition, 0, 3)=='978')){
            $rslt = DB::select('books', '`isbn` = '.DB::get_str($condition),'`id` '.$o);
        // Then the unique book id
        }elseif(is_numeric($condition)) {
            $rslt = DB::select('books', '`id` = '. intval($condition),'`id` '.$o);
        }else{
            $cond = preg_replace('/\*+/', '%', DB::get_str($condition));
            $o = in_array($order, ['ASC','DESC']) ? $order : 'DESC';
            $wordc = count(explode(' ', $condition));
            // Test whether is book name
            if (strlen($cond) < 100 && $wordc < 3) {
                $rslt = DB::select('books',   '`name` LIKE ' .$cond, '`id` ' . $o);
            // Find in the description
            }else{
                $cond = str_replace('/\s+/', '%', $cond);
                $rslt = DB::select('books',   '`name` LIKE ' .$cond, '`id` ' . $o);
            }
        }
        return $this->_get_display_from_data($rslt);
    }
}
